import Taro from '@tarojs/taro'
import Enum from '../../constants/Enum'

const api_host = `https://xcxbaige.huanbile.com/wx/v1.0.0`
// const api_host = `http://xcxbaige.huanbile.com:9090/wx/v1.0.0`


const api = ({url, data, headerParam, method, dataType}) => {
  return new Promise((resolve, reject) => {
    let header = {}
    let loginData = Taro.getStorageSync(Enum.session_key)
    if (loginData) {
      loginData = JSON.parse(loginData)
      header['session-key'] = loginData.session_key
    }

    if (headerParam) {
      header = {
        ...header,
        ...headerParam
      }
    }

    Taro.request({
      url: `${api_host}${url}`,
      header: header,
      method: method ? method : 'GET',
      data: data ? data : {},
      dataType: dataType ? dataType : '',
      success: res => {
        if (res.statusCode === 200 || res.statusCode === 404 || res.statusCode === 204 || res.data.status === 1) {
          resolve(res)
        } else {
          reject(res)
        }
      },
      fail: err => {
        reject(err)
      }
    })

  })
}

export default api
